package com.jeesite.modules.kj.api.vo.request;

import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.kj.entity.KjActivity;
import com.jeesite.modules.kj.entity.KjCity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Calendar;
import java.util.Date;

@Data
public class ActivityListRequestVo {
    @ApiModelProperty(name="cityId", value="城市ID，不填默认全部城市", example = "000111")
    String cityId;

    @ApiModelProperty(name="activityName", value ="活动名", example = "爱心活动")
    String activityName;

    @ApiModelProperty(name="pageNo", value ="页码，默认是1", example = "1")
    int pageNo;

    @ApiModelProperty(name="pageSize", value ="每页记录数，默认是10", example = "15")
    int pageSize;


    @ApiModelProperty(name="activityTime", value ="活动时间", example = "2020-10")
    Date activityTime;


    public KjActivity convertToEntity(){
        KjActivity where = new KjActivity();
        where.setActivityAudit("1");
        if(StringUtils.isNotBlank(activityName)) {
            where.setName(activityName);
        }
        if(StringUtils.isNotBlank(cityId)) {
            KjCity kjCity = new KjCity();
            kjCity.setId(cityId);
            where.setKjCity(kjCity);
        }
//        where.setActivityStatusNotIn(new String[]{KjActivity.NOT_BEGIN});
        if(activityTime != null){
            where.setStartSignTime_gte(getMonthBegin(activityTime));
            where.setStartSignTime_lte(getMonthEnd(activityTime));
        }
        Page page = new Page(pageNo, pageSize);
        where.setPage(page);
        return where;
    }

    public Date getMonthBegin(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //设置为1号,当前日期既为本月第一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        //将小时置为0
        cal.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟置为0
        cal.set(Calendar.MINUTE, 0);
        //将秒置为0
        cal.set(Calendar.SECOND,0);
        //将毫秒置为0
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public Date getMonthEnd(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //设置为当月最后一天
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时置为23
        cal.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟置为59
        cal.set(Calendar.MINUTE, 59);
        //将秒置为59
        cal.set(Calendar.SECOND,59);
        //将毫秒置为999
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }
}
